﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using Common;


namespace Client
{
    public partial class FormSettings : Form
    {
        public FormSettings()
        {
            InitializeComponent();
            _ConfigTransfer = new ConfigTransfer();
            _ConfigTransfer._lat = 45.4248;
            _ConfigTransfer._long = -75.6989;
        }

        private ConfigTransfer _ConfigTransfer;
        public ConfigTransfer Config
          
        {
            get { return _ConfigTransfer; }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            _ConfigTransfer._dual = radioButton1.Checked;
            _ConfigTransfer._lat = DegreesMinuteSecondDecimalDegrees(maskedTextBoxLat.Text);
            _ConfigTransfer._long = DegreesMinuteSecondDecimalDegrees(maskedTextBoxLong.Text);
            _ConfigTransfer._eastAz = double.Parse(maskedTextBoxEast.Text);
            _ConfigTransfer._westAz = double.Parse(maskedTextBoxWest.Text);
            _ConfigTransfer._maxElevation = double.Parse(maskedTextBoxUp.Text);
            _ConfigTransfer._minElevation = double.Parse(maskedTextBoxDown.Text);
            _ConfigTransfer._offsetToUTC = int.Parse(maskedTextBoxUTC.Text);
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        // Returns decimal latitude or longitude from deg, min, sec entered in the configuration
        public static double DegreesMinuteSecondDecimalDegrees(string latLong)
        {
            var neg = 0;
            if (latLong[0] == '-')
            {
                neg = 1;
                latLong = latLong.Substring(1);
            }
            var parts = latLong.Split('.');
            double degs = 0;
            if (parts.Length > 0)
                degs = Double.Parse(parts[0]);
            double mins = 0;
            if (parts.Length > 1)
                mins = Double.Parse(parts[1]);
            double secs = 0;
            if (parts.Length > 2)
                secs = Double.Parse(parts[2]);
            if (neg != 1)
            {
                return degs + (mins / 60) + (secs / 3600);
            }
            if (neg == 1)
            {
                return degs - (mins / 60) - (secs / 3600);
            }
            return -9999;
        }

        private void maskedTextBoxLat_Validating(object sender, CancelEventArgs e)
        {
                _ConfigTransfer._lat = DegreesMinuteSecondDecimalDegrees(maskedTextBoxLat.Text);
        }



        private void button4_Click(object sender, EventArgs e)
        {
            var map = new Map();
            map.Lat = _ConfigTransfer._lat;
            map.Long = _ConfigTransfer._long;
            if (map.ShowDialog(this) == DialogResult.OK)
            {
                
            }
        }
    }
}
